﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WindowsGame1.Objects3D
{
    class clsManualCube
    {
        Matrix world;
        VertexPositionNormalTexture[] verts;
        VertexBuffer vertexBuffer;
        GraphicsDevice GraphicsDevice;
        Matrix worldRotation = Matrix.Identity;
        Matrix worldTranslation = Matrix.Identity;
        Matrix worldScale = Matrix.Identity;
        BasicEffect effect;
        Texture2D texture;
        float x, y, z;
        public clsManualCube(GraphicsDevice _GraphicsDevice,float _x,float _y,float _z, float width, float height, float depth, Texture2D _texture)
        {
            texture = _texture;
            GraphicsDevice = _GraphicsDevice;
            x = _x;
            y = _y;
            z = _z;
            world =  Matrix.CreateTranslation(new Vector3(x,y,z));
            Vector3 tlf = new Vector3(x, y + height, z); // Topo Esquerdo da Frente
            Vector3 blf = new Vector3(x, y, z); // Fundo Esquerdo da Frente
            Vector3 trf = new Vector3(x + width, y + height, z); // Topo Direito da Frente
            Vector3 brf = new Vector3(x + width, y, z); // Fundo Direito da Frente
            Vector3 tlb = new Vector3(x, y + height, z + depth); // Topo Esquerdo de tras
            Vector3 trb = new Vector3(x + width, y + height, z + depth); // Topo Direito de tras
            Vector3 blb = new Vector3(x, y, z + depth); // Fundo Esquerdo de tras
            Vector3 brb = new Vector3(x + width, y, z + depth); // Fundo Direito de tras

            Vector3 frontNormal = new Vector3(x+width*.5f, y+height*.5f, z+depth) ;
            Vector3 backNormal = new Vector3(x + width * .5f, y + height * .5f, z);
            Vector3 topNormal = new Vector3(x + width * .5f, y + height, z+depth*.5f);
            Vector3 bottomNormal = new Vector3(x + width * .5f, y, z+depth*.5f);
            Vector3 leftNormal = new Vector3(x, y+height*.5f, z+depth*.5f) ;
            Vector3 rightNormal = new Vector3(x+width, y+height*.5f, z+depth*.5f) ;

            frontNormal = backNormal = topNormal = bottomNormal = leftNormal = rightNormal = new Vector3(1.0f, 1.0f, 1.0f);
            Vector2 textureTopLeft = new Vector2(0.5f , 0.0f );
            Vector2 textureTopRight = new Vector2(0.0f , 0.0f );
            Vector2 textureBottomLeft = new Vector2(0.5f , 0.5f );
            Vector2 textureBottomRight = new Vector2(0.0f , 0.5f);
           /* verts = new[] {
       
        new VertexPositionColor(tlf, Color.Gray),
        new VertexPositionColor(blf, Color.Gray),
        new VertexPositionColor(trf, Color.Gray),
        new VertexPositionColor(blf, Color.Gray),
        new VertexPositionColor(brf, Color.Gray),
        new VertexPositionColor(trf, Color.Gray),

  
        new VertexPositionColor(tlb, Color.Gray),
        new VertexPositionColor(trb, Color.Gray),
        new VertexPositionColor(blb, Color.Gray),
        new VertexPositionColor(blb, Color.Gray),
        new VertexPositionColor(trb, Color.Gray),
        new VertexPositionColor(brb, Color.Gray),

      
        new VertexPositionColor(tlf, Color.Gray),
        new VertexPositionColor(trb, Color.Gray),
        new VertexPositionColor(tlb, Color.Gray),
        new VertexPositionColor(tlf, Color.Gray),
        new VertexPositionColor(trf, Color.Gray),
        new VertexPositionColor(trb, Color.Gray),

       
        new VertexPositionColor(blf, Color.Gray),
        new VertexPositionColor(blb, Color.Gray),
        new VertexPositionColor(brb, Color.Gray),
        new VertexPositionColor(blf, Color.Gray),
        new VertexPositionColor(brb, Color.Gray),
        new VertexPositionColor(brf, Color.Gray),

       
        new VertexPositionColor(tlf, Color.Gray),
        new VertexPositionColor(blb, Color.Gray),
        new VertexPositionColor(blf, Color.Gray),
        new VertexPositionColor(tlb, Color.Gray),
        new VertexPositionColor(blb, Color.Gray),
        new VertexPositionColor(tlf, Color.Gray),

        
        new VertexPositionColor(trf, Color.Gray),
        new VertexPositionColor(brf, Color.Gray),
        new VertexPositionColor(brb, Color.Gray),
        new VertexPositionColor(trb, Color.Gray),
        new VertexPositionColor(trf, Color.Gray),
        new VertexPositionColor(brb, Color.Gray),
    };*/

            verts = new VertexPositionNormalTexture[36];
            // Front face.
            verts[0] = new VertexPositionNormalTexture(
                tlf, frontNormal, textureTopLeft);
            verts[1] = new VertexPositionNormalTexture(
                blf, frontNormal, textureBottomLeft);
            verts[2] = new VertexPositionNormalTexture(
                trf, frontNormal, textureTopRight);
            verts[3] = new VertexPositionNormalTexture(
                blf, frontNormal, textureBottomLeft);
            verts[4] = new VertexPositionNormalTexture(
                brf, frontNormal, textureBottomRight);
            verts[5] = new VertexPositionNormalTexture(
                trf, frontNormal, textureTopRight);

            // Back face.
            verts[6] = new VertexPositionNormalTexture(
                tlb, backNormal, textureTopRight);
            verts[7] = new VertexPositionNormalTexture(
                trb, backNormal, textureTopLeft);
            verts[8] = new VertexPositionNormalTexture(
                blb, backNormal, textureBottomRight);
            verts[9] = new VertexPositionNormalTexture(
                blb, backNormal, textureBottomRight);
            verts[10] = new VertexPositionNormalTexture(
                trb, backNormal, textureTopLeft);
            verts[11] = new VertexPositionNormalTexture(
                brb, backNormal, textureBottomLeft);

            // Top face.
            verts[12] = new VertexPositionNormalTexture(
                tlf, topNormal, textureBottomLeft);
            verts[13] = new VertexPositionNormalTexture(
                trb, topNormal, textureTopRight);
            verts[14] = new VertexPositionNormalTexture(
                tlb, topNormal, textureTopLeft);
            verts[15] = new VertexPositionNormalTexture(
                tlf, topNormal, textureBottomLeft);
            verts[16] = new VertexPositionNormalTexture(
                trf, topNormal, textureBottomRight);
            verts[17] = new VertexPositionNormalTexture(
                trb, topNormal, textureTopRight);

            // Bottom face. 
            verts[18] = new VertexPositionNormalTexture(
                blf, bottomNormal, textureTopLeft);
            verts[19] = new VertexPositionNormalTexture(
                blb, bottomNormal, textureBottomLeft);
            verts[20] = new VertexPositionNormalTexture(
                brb, bottomNormal, textureBottomRight);
            verts[21] = new VertexPositionNormalTexture(
                blf, bottomNormal, textureTopLeft);
            verts[22] = new VertexPositionNormalTexture(
                brb, bottomNormal, textureBottomRight);
            verts[23] = new VertexPositionNormalTexture(
                brf, bottomNormal, textureTopRight);

            // Left face.
            verts[24] = new VertexPositionNormalTexture(
                tlf, leftNormal, textureTopRight);
            verts[25] = new VertexPositionNormalTexture(
                blb, leftNormal, textureBottomLeft);
            verts[26] = new VertexPositionNormalTexture(
                blf, leftNormal, textureBottomRight);
            verts[27] = new VertexPositionNormalTexture(
                tlb, leftNormal, textureTopLeft);
            verts[28] = new VertexPositionNormalTexture(
                blb, leftNormal, textureBottomLeft);
            verts[29] = new VertexPositionNormalTexture(
                tlf, leftNormal, textureTopRight);

            // Right face. 
            verts[30] = new VertexPositionNormalTexture(
                trf, rightNormal, textureTopLeft);
            verts[31] = new VertexPositionNormalTexture(
                brf, rightNormal, textureBottomLeft);
            verts[32] = new VertexPositionNormalTexture(
                brb, rightNormal, textureBottomRight);
            verts[33] = new VertexPositionNormalTexture(
                trb, rightNormal, textureTopRight);
            verts[34] = new VertexPositionNormalTexture(
                trf, rightNormal, textureTopLeft);
            verts[35] = new VertexPositionNormalTexture(
                brb, rightNormal, textureBottomRight);



            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture),
            verts.Length, BufferUsage.WriteOnly);

            vertexBuffer.SetData(verts);

            effect = new BasicEffect(GraphicsDevice);
        }

      
        public  void Draw()
        {
            GraphicsDevice.SetVertexBuffer(vertexBuffer);

            effect.World = world;
            effect.Texture = texture;
            effect.TextureEnabled = true;
            effect.LightingEnabled = true;
            effect.View = Camera.View;
            effect.Projection = Camera.Projection;
      
            foreach (var pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                 effect.View = Camera.View;
                    effect.Projection = Camera.Projection;
                GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList,
                    verts, 0, 12);
            }
        }

        public float getZ()
        {
            return z; 
        }

        public float getX()
        {
            return x;
        }

        public float getY()
        {
            return y;
        }
    }
}
